package com.dameng.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dameng.system.entity.SystemMenu;
import com.dameng.system.entity.SystemRoleMenu;
import com.dameng.system.service.SystemMenuService;
import com.dameng.system.service.SystemRoleMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/systemMenu")
public class SystemMenuController {

    @Resource
    SystemMenuService systemMenuService;

    @Resource
    SystemRoleMenuService systemRoleMenuService;

    @GetMapping("/manager")
    public String manager() {
        return "/system/Menu/manager";
    }

    @PostMapping("/menuList")
    @ResponseBody
    public List<SystemMenu> menuList() {
        QueryWrapper<SystemMenu> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByAsc(SystemMenu::getSortNo);
        return systemMenuService.list(wrapper);
    }

    @PostMapping("/list")
    @ResponseBody
    public Page<SystemMenu> list(int page, int limit, SystemMenu systemMenu) {
        QueryWrapper<SystemMenu> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(systemMenu.getPid())) {
            wrapper.lambda().eq(SystemMenu::getPid, systemMenu.getPid());
        }
        if (StringUtils.isNotEmpty(systemMenu.getName())) {
            wrapper.lambda().like(SystemMenu::getName, systemMenu.getName());
        }
        wrapper.lambda().orderByAsc(SystemMenu::getSortNo);
        Page<SystemMenu> page1 = new Page<>(page, limit);
        return systemMenuService.page(page1, wrapper);
    }

    @GetMapping("/iconfont")
    public String iconfont() {
        return "/system/Menu/iconfont";
    }

    @GetMapping("/add")
    public String add(ModelMap model, SystemMenu systemMenu) {
        model.addAttribute("systemMenu", systemMenu);
        model.addAttribute("pName", systemMenuService.getById(systemMenu.getPid()).getName());
        model.addAttribute("type", "save");
        return "/system/Menu/edit";
    }

    @PostMapping("/save")
    @ResponseBody
    @Transactional
    public Boolean save(SystemMenu systemMenu) {
        if (StringUtils.isEmpty(systemMenu.getUrlRequest())){
            systemMenu.setUrlRequest("/");
        }
        systemMenu.setMenuStyle(systemMenu.getMenuStyle().trim());
        return systemMenuService.save(systemMenu);
    }

    @GetMapping("/edit")
    public String edit(ModelMap model, String menuId) {
        SystemMenu systemMenu = systemMenuService.getById(menuId);
        model.addAttribute("systemMenu", systemMenu);
        model.addAttribute("pName", systemMenuService.getById(systemMenu.getPid()).getName());
        model.addAttribute("type", "update");
        return "/system/Menu/edit";
    }

    @PostMapping("/update")
    @ResponseBody
    @Transactional
    public Boolean update(SystemMenu systemMenu) {
        systemMenu.setMenuStyle(systemMenu.getMenuStyle().trim());
        return systemMenuService.updateById(systemMenu);
    }

    @PostMapping("/delete")
    @ResponseBody
    @Transactional
    public Boolean delete(String... menuIds) {
        List<String> ids = Arrays.asList(menuIds);
        List<String> idList = new ArrayList<>();
        for (String menuId : ids) {
            List<String> menus = systemMenuService.childMenuByMenuId(menuId);
            idList.add(menuId);
            idList.addAll(menus);
        }

        QueryWrapper<SystemRoleMenu> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(SystemRoleMenu::getMenuId, idList);
        systemRoleMenuService.remove(wrapper);

        return systemMenuService.removeByIds(idList);
    }

    @PostMapping("/enabled")
    @ResponseBody
    @Transactional
    public boolean enabled(boolean enabled, String... menuIds) {
        SystemMenu systemMenu = new SystemMenu();
        systemMenu.setStatus(enabled);

        List<String> ids = Arrays.asList(menuIds);
        UpdateWrapper<SystemMenu> wrapper = new UpdateWrapper<>();
        wrapper.lambda().in(SystemMenu::getMenuId, ids);
        return systemMenuService.update(systemMenu, wrapper);
    }
}
